package com.kb.design.util;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

public class DateUtils {


    /**
     * 根据给定日期，生成前后各15天的日期列表（共30天）
     *
     * @param currentDate 当前日期
     * @return 日期字符串列表，格式：yyyy/MM/dd
     */
    public static List<String> generateDateList(LocalDate currentDate) {
        List<String> dateList = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");

        // 向前推15天
        for (int i = -15; i <= 14; i++) {
            LocalDate targetDate = currentDate.plusDays(i);
            dateList.add(targetDate.format(formatter));
        }

        return dateList;
    }


    /**
     * 获取给定日期在固定区间中的位置索引
     *
     * @param inputDate 输入日期
     * @return 索引值，范围 [0, 30]
     */
    public static int getPositionInDateRange(LocalDate inputDate) {
        List<String> dateList = generateDateList(LocalDate.now());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        LocalDate START_DATE = LocalDate.parse(dateList.get(0), formatter);
        LocalDate END_DATE = START_DATE.plusDays(29); // 共30天

        if (!inputDate.isAfter(START_DATE) || inputDate.isEqual(START_DATE)) {
            return 0;
        } else if (!inputDate.isBefore(END_DATE) || inputDate.isEqual(END_DATE)) {
            return 29;
        } else {
            return (int) ChronoUnit.DAYS.between(START_DATE, inputDate);
        }
    }

    public static LocalDateTime getStartOfCurrentQuarter(LocalDateTime now) {
        int quarter = now.getMonthValue() / 3 + (now.getMonthValue() % 3 == 0 ? 0 : 1);
        return YearMonth.of(now.getYear(), quarter * 3 - 2)
                .atDay(1)
                .atStartOfDay();
    }

    public static LocalDateTime getEndOfCurrentQuarter(LocalDateTime now) {
        int quarter = now.getMonthValue() / 3 + (now.getMonthValue() % 3 == 0 ? 0 : 1);
        LocalDate lastDayOfQuarter = YearMonth.of(now.getYear(), quarter * 3).atEndOfMonth();
        return LocalDateTime.of(lastDayOfQuarter, LocalTime.MAX);
    }
    /**
     * 获取上个月的第一天和最后一天，格式为 LocalDateTime
     *
     * @return [0]第一天开始时间, [1]最后一天结束时间
     */
    public static LocalDate[] getFirstAndLastDayOfLastMonth() {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 获取上个月
        YearMonth previousMonth = YearMonth.from(currentDate).minusMonths(1);

        // 上个月的第一天
        LocalDate firstDayOfPreviousMonth = previousMonth.atDay(1);

        // 上个月的最后一天
        LocalDate lastDayOfPreviousMonth = previousMonth.atEndOfMonth();
        return new LocalDate[]{
                firstDayOfPreviousMonth,
                lastDayOfPreviousMonth
        };
    }

    /**
     * 获取指定年月的 LocalDate 对象，返回该月份的第一天和最后一天
     *
     * @param date 指定的 LocalDate 日期，例如 2025-09-15
     * @return LocalDate[] [0] 为当月第一天, [1] 为当月最后一天
     */
    public static LocalDate[] getFirstAndLastDayOfMonth(LocalDate date) {
        YearMonth yearMonth = YearMonth.from(date);

        LocalDate firstDay = yearMonth.atDay(1);
        LocalDate lastDay = yearMonth.atEndOfMonth();

        return new LocalDate[]{firstDay, lastDay};
    }

    /**
     * 获取指定日期所在年份的第一天和最后一天
     *
     * @param date 指定的 LocalDate 日期，例如 2025-09-15
     * @return LocalDate[] [0] 为当年第一天, [1] 为当年最后一天
     */
    public static LocalDate[] getFirstAndLastDayOfYear(LocalDate date) {
        Year year = Year.from(date);

        LocalDate firstDay = year.atDay(1); // 当年第一天
        LocalDate lastDay = year.atMonth(Month.DECEMBER).atEndOfMonth(); // 当年最后一天

        return new LocalDate[]{firstDay, lastDay};
    }
}
